﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedComponents;

namespace OurLeague.Object
{
    public class Person
    {
        #region Fields

        String _name;
        String _surname;
        DateTime _birthday;
        String _birthplace;
        String _nationality;
        String _id;

        public static List<Person> Persons = new List<Person>();

        #endregion

        #region Constructors

        /*
        public Person(String name, String surname, DateTime birthday, String birthplace, String nationality, String personID)
        {

        }
        */

        public Person() { }

        public Person(Person person)
        {
            Id = person.Id;
            Name = person.Name;
            Surname = person.Surname;
            Birthday = person.Birthday;
            Birthplace = person.Birthplace;
            Nationality = person.Nationality;
        }

        #endregion

        #region Public Methods

        public bool delete()
        {
            if (!MySqlQueryHelper.deleteQuery(MySqlManager.Connection, "Persons", "personId", new String[] { Id }))
                return false;   
            
            Persons.Remove(this);
            return true;
        }

        public void save()
        {
            MySqlRecord updateRecord = new MySqlRecord();

            updateRecord["firstName"] = Name;
            updateRecord["secondName"] = Surname;
            updateRecord["born"] = Birthday;
            updateRecord["nationality"] = Nationality;
            updateRecord["birthPlace"] = Birthplace;

            if (!Persons.Exists(p => p.Id == Id))
            {
                Persons.Add(this);
                Id = MySqlQueryHelper.saveQuery(MySqlManager.Connection, "Persons", null, updateRecord).ToString();

                return;
            }

            MySqlQueryHelper.saveQuery(MySqlManager.Connection, "Persons", "personId=" + Id, updateRecord);
        }

        /*
        public void update(String str)
        {

        }

        public void view()
        {

        }
        */
        #endregion

        #region Properties

        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public String Surname
        {
            get { return _surname; }
            set { _surname = value; }
        }

        public DateTime Birthday
        {
            get { return _birthday; }
            set { _birthday = value; }
        }

        public String Birthplace
        {
            get { return _birthplace; }
            set { _birthplace = value; }
        }

        public String Nationality
        {
            get { return _nationality; }
            set { _nationality = value; }
        }

        public String Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public String SurnameAndN
        {
            get { return _surname + " " + _name.Substring(0, 1) + "."; }
        }

        #endregion

        #region Static Methods

        public static void loadAll()
        {
            var queryResult = MySqlQueryHelper.selectQuery(MySqlManager.Connection, "Persons", null, null);
            foreach (MySqlRecord personRecord in queryResult)
            {
                if (Persons.Find(p => p.Id == personRecord["personId"].ToString()) == null)
                    addNewPerson(personRecord);
            }
        }

        private static Person addNewPerson(MySqlRecord personRecord)
        {
            Person person = new Person();
            person.Id = personRecord["personId"].ToString();
            person.Name = personRecord["firstName"].ToString();
            person.Surname = personRecord["secondName"].ToString();
            person.Birthday = DateTime.Parse(personRecord["born"].ToString());
            person.Birthplace = personRecord["birthPlace"].ToString();
            person.Nationality = personRecord["nationality"].ToString();

            Persons.Add(person);

            return person;
        }

        public static Person load(String idPerson)
        {
            Person person = Persons.Find(p => p.Id == idPerson);

            if (person == null)
            {
                var queryResult = MySqlQueryHelper.selectQuery(MySqlManager.Connection, "Persons", null, "personId=" + idPerson);
                person = addNewPerson(queryResult[0]);
            }

            return person;
        }

        #endregion
    }
}
